use std::str::FromStr;

use crate::http::error::HttpParseError;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HttpVersion {
    V1_0,
    V1_1,
}

impl Default for HttpVersion {
    fn default() -> Self {
        Self::V1_1
    }
}

impl FromStr for HttpVersion {
    type Err = HttpParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "HTTP/1.0" => Ok(Self::V1_0),
            "HTTP/1.1" => Ok(Self::V1_1),
            _ => Err(HttpParseError::UnsupportedHttpVersion(
                "Unsupported HTTP version.",
            )),
        }
    }
}

impl ToString for HttpVersion {
    fn to_string(&self) -> String {
        match self {
            Self::V1_0 => "HTTP/1.0".into(),
            Self::V1_1 => "HTTP/1.1".into(),
        }
    }
}